package com.waimai.service.imlp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.waimai.Dao.SetmealMapper;
import com.waimai.Dto.SetmealDto;
import com.waimai.common.CustomException;
import com.waimai.domain.Setmeal;
import com.waimai.domain.SetmealDish;
import com.waimai.service.SetmealDishService;
import com.waimai.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class SetmealServiceImlp extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    SetmealDishService setmealDishService;

    @Override
    @Transactional
    //这个方法有问题  不知道具体原因在哪
    public void sss(SetmealDto setmealDto) {
        this.save(setmealDto);
        List<SetmealDish> setmealDishes=setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item)-> {
           item.setSetmealId(setmealDto.getId());

            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
        }

    @Override
    @Transactional
    public void romesss(List<Long> ids) {
        //构造器
        LambdaQueryWrapper<Setmeal>  queryWrapper =new LambdaQueryWrapper<>();
        //设置条件
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);
        //统计
        int count = this.count(queryWrapper);
        if (count>0){
             throw new CustomException("删除失败");
        }
           //可以删除则
        this.removeByIds(ids);
        /////
        LambdaQueryWrapper<SetmealDish> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(queryWrapper1);



    }


}
